package com.thingcom.delinrobot.utils;//修改名字弹框

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import com.thingcom.delinrobot.R;

import java.util.Objects;


public class CommonDialog extends DialogFragment {
    private setOnOkListener listener;
    private setOnCancerListener listener1;
    private static final String DIALOG_TITLE = "title";
    private static final String DIALOG_CONTENT = "content";

    public static CommonDialog newInstance(String title, String content) {

        Bundle args = new Bundle();
        args.putString(DIALOG_TITLE,title);
        args.putString(DIALOG_CONTENT,content);
        CommonDialog fragment = new CommonDialog();
        fragment.setArguments(args);
        return fragment;
    }

    public void setOnOkListener(setOnOkListener listener){
        this.listener = listener;
    }

    public void setOnCancerListener(setOnCancerListener listener){
        this.listener1 = listener;
    }
    public interface setOnOkListener {
        void onFinish();
    }

    public interface setOnCancerListener {
        void onFinish();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Objects.requireNonNull(getDialog()).requestWindowFeature(Window.FEATURE_NO_TITLE);
        Objects.requireNonNull(getDialog().getWindow()).setBackgroundDrawableResource(android.R.color.transparent);
        View view = inflater.inflate(R.layout.dialog_content_layout,container,false);
        Bundle bundle = getArguments();
        TextView tvTitle = view.findViewById(R.id.tv_title_dialog);
        TextView tvContent = view.findViewById(R.id.tv_content_dialog);
        if (bundle != null) {
            tvTitle.setText(bundle.getString(DIALOG_TITLE));
            tvContent.setText(bundle.getString(DIALOG_CONTENT));
        }

        Button buttonOk = view.findViewById(R.id.dialog_confirm);

        buttonOk.setOnClickListener(v -> {
            if (listener != null){
                listener.onFinish();
                dismiss();
            }
        });
        Button buttonConcel = view.findViewById(R.id.dialog_cancel);

        buttonConcel.setOnClickListener(v ->{
            if (listener1 != null){
                listener1.onFinish();
                dismiss();
            } else {
                dismiss();
            }
        });

        return view;
    }



}
